﻿/**********************************************/
/*Project: CRF#                               */
/*Author: Zhongkai Fu                         */
/*Email: fuzhongkai@gmail.com                 */
/**********************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRFSharp
{
    public class CRF_t
    {
        /**
         * @brief 加载训练模型，并且将模型赋值给tagger
        **/
        public int crf_load_model(ref CrfModel crf_model, string model_name)
        {
            int ret = 0;
            ret = crf_model.load_model(model_name);
            if (ret < 0)
            {
                return -1;
            }

            return 0;
        }



        /**
         * @brief 为crf_out_t分配内存，为每个crf_term_out_t分配tsize内存
        **/
        public crf_out_t crf_create_out(uint tsize)
        {
            crf_out_t crf_out = null;

            //1.为crf_out分配内存
            crf_out = new crf_out_t();

            //2.为crf_term_out分配内存
            for (int i = 0; i < Utils.MAX_NBEST_NUM; i++)
            {
                crf_term_out_t crf_term = crf_out.term_buf[i];

                //3.为crftermoffsets分配内存
                crf_term.crftermoffsets = new List<int>((int)tsize + 1);

                //4.为crftermpos分配内存
                crf_term.crftermlen = new List<int>((int)tsize + 1);

                //7.为neprop分配内存
                crf_term.crfneprop = new List<neprop_t>((int)tsize + 1);

                //初始化
                crf_term.wordtotallen = 0;
                crf_term.crftermcount = 0;

                for (int j = 0; j < tsize; j++)
                {
                    crf_term.crftermoffsets.Add(0);
                    crf_term.crftermlen.Add(0);

                    crf_term.crfneprop.Add(new neprop_t());
                    crf_term.crfneprop[j].strTag = "";
                    crf_term.crfneprop[j].iWeight = 0;

                }
            }

            return crf_out;
        }

      
        public int crf_segment_words(ref SegCrfTag tagger, ref crf_out_t pout, ref List<List<string>> inbuf, int nbest_value, int vlevel_value)
        {
            int ret = 0;
            ret = tagger.reset();
            if (ret < 0)
            {
                return -1;
            }

            ret = tagger.seg_add(ref inbuf);
            if (ret < 0)
            {
                return -1;
            }

            //设置nbest，vlevel
            if (nbest_value > Utils.MAX_NBEST_NUM)
            {
                nbest_value = Utils.MAX_NBEST_NUM;
            }

            tagger.set_nbest((uint)nbest_value);
            tagger.set_vlevel((uint)vlevel_value);
            //4.parse
            ret = tagger.seg_parse();
            if (ret < 0)
            {
                return -1;
            }

            //5.my_output
            ret = tagger.seg_output(ref pout);
            if (ret < 0)
            {
                return -1;
            }

            return 0;
        }
    }
}
